package com.cgly.medical.listener;

import com.cgly.medical.config.XDelayedMessageConfig;
import com.cgly.medical.domain.DrugOrder;
import com.cgly.medical.model.DrugOrderModel;
import com.cgly.medical.service.DrugOrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author wangxiangli
 * @create 2022-08-22-16:35
 */
@Component
@RabbitListener(queues = XDelayedMessageConfig.DIRECT_QUEUE)
public class DelayedMQReciever {


    @Autowired
    private DrugOrderService drugOrderService;

    @RabbitHandler
    public void process(String endOrderId, Channel channel){
        System.out.println("DelayedMQReciever接收到的消息是："+ endOrderId);
        //这里来实现业务逻辑
        //拿着传过来的订单id对向数据查询，然后判断订单的支付状态，
        //如果为0未支付，就将订单状态改为3取消订单
        //如果为1已支付，就不做操作
//        int orderId=Integer.valueOf(message);
//        DrugOrder drugOrder=drugOrderService.getDrugOrderById(orderId);
//        if(drugOrder.getStatus()==0){
//            drugOrder.setStatus(2);
//            DrugOrderModel drugOrderModel=new DrugOrderModel();
//            BeanUtils.copyProperties(drugOrder, drugOrderModel);
//            drugOrderService.updateDrugOrder(drugOrderModel);
//        }
        List<DrugOrder> drugOrders = drugOrderService.getDrugOrderByEndOrderId(endOrderId);
        for(DrugOrder drugOrder:drugOrders){
            if(drugOrder.getStatus()==0){
                drugOrder.setStatus(2);
                DrugOrderModel drugOrderModel=new DrugOrderModel();
                BeanUtils.copyProperties(drugOrder, drugOrderModel);
                drugOrderService.updateDrugOrder(drugOrderModel);
            }
        }
         System.out.println("订单支付超时，取消了。。。。");
    }
}
